package com.itheima.controller;

import com.itheima.anno.LogOperate;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/emps")

public class EmpController {

    @Autowired
    private EmpService empService;
    /**
     * 分页查询，使用对象接收前端参数?key=value&key2=value2...
     * 对象的属性名要与参数的名称一致，否则接收不到
     * @param empQueryParam
     * @return
     */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        log.info("分页查询，参数：{}", empQueryParam);
        // 调用业务分页查询
        PageResult<Emp> pageResult=empService.pageQuery(empQueryParam);
        return Result.success(pageResult);
    }
    /**
     * 添加员工
     */
    @PostMapping
    @LogOperate
    public Result save(@RequestBody Emp emp){
        log.info("新增员工，员工信息：{}", emp);
        empService.save(emp);
        return Result.success();
    }
    /**
     * 根据id删除员工
     */
    @DeleteMapping("/{id}")
    @LogOperate
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除员工，id为：{}", id);
        empService.deleteById(id);
        return Result.success();
    }
    /**
     * 查询全部员工
     */
    @GetMapping("/list")
    public Result list(){
        log.info("查询全部员工");
        List<Emp> empList = empService.findAll();
        return Result.success(empList);
    }
    /**
     * 批量删除员工
     */
    @DeleteMapping
    @LogOperate
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除部门: ids={} ", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }
    /**
     * 根据id查询员工
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工，id为：{}", id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }
    /**
     * 修改员工
     */
    @PutMapping
    @LogOperate
    public Result update(@RequestBody Emp emp){
        log.info("修改员工，员工信息为：{}", emp);
        empService.update(emp);
        return Result.success();
    }

}